package com.daJiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daJiao.constants.Constants;
import com.daJiao.domain.*;
import com.daJiao.entity.SysPatient;
import com.daJiao.entity.SysRole;
import com.daJiao.enums.DeleteFlagEnums;
import com.daJiao.holder.SecurityUserHolder;
import com.daJiao.service.SysPatientService;
import com.daJiao.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>Title:	  SysPatientController. </p>
 * <p>Description 患者管理 </p>
 * <p>Company:   医百科技 </p>
 *
 * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
 * @CreateDate 2021 /12/10 19:05
 */
@RestController
@RequestMapping(value = "/sysPatient")
@Api(tags = "患者管理")
public class SysPatientController {

    @Resource
    private SysPatientService sysPatientService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * <p>Title:	  listPage. </p>
     * <p>Description 分页查询患者列表</p>
     *
     * @param pageParam the role
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2017 /8/17 19:04
     */
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "分页查询患者列表")
    public Wrapper<Page<SysPatient>> page(@ApiParam(name = "sysPatient", value = "患者信息") @RequestBody PageParam<SysPatient> pageParam) {
        Page<SysPatient> listPage = sysPatientService.listPage(pageParam);
        return WrapMapper.wrap(listPage);
    }


    /**
     * <p>Title:      update. </p>
     * <p>Description 修改患者信息</p>
     *
     * @param sysPatient the SysPatient
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/20 15:38
     */
    @PutMapping
    @ApiOperation(httpMethod = "POST", value = "修改患者信息")
    public Wrapper<Boolean> update(@ApiParam(name = "sysPatient", value = "患者信息") @RequestBody SysPatient sysPatient) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        sysPatient.setUpdateBy(securityUser.getUserId());
        sysPatient.setUpdater(securityUser.getRealName());
        sysPatient.setUpdateTime(LocalDateTime.now());
        return WrapMapper.ok(sysPatientService.updateById(sysPatient));
    }

    /**
     * <p>Title:      save. </p>
     * <p>Description 保存患者信息</p>
     *
     * @param sysPatient the SysPatient
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2018 /2/22 17:24
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "保存患者信息")
    public Wrapper<Boolean> save(@ApiParam(name = "sysPatient", value = "患者信息") @RequestBody SysPatient sysPatient) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        sysPatient.setCreateBy(securityUser.getUserId());
        sysPatient.setCreator(securityUser.getRealName());
        sysPatient.setUpdateBy(securityUser.getUserId());
        sysPatient.setUpdater(securityUser.getRealName());
        return WrapMapper.ok(sysPatientService.save(sysPatient));
    }

    /**
     * <p>Title:      getRoleList. </p>
     * <p>Description 查詢所有患者信息</p>
     *
     * @return List<SysRole>
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/11 14:35
     */
    @GetMapping(value = "/list")
    @ApiOperation(httpMethod = "GET", value = "查询患者信息")
    public Wrapper<List<SysPatient>> list() {
        List<SysPatient> sysPatients = sysPatientService.list(new QueryWrapper<SysPatient>().lambda().eq(SysPatient::getDelFlag, DeleteFlagEnums.NORMAL.getStatus()).eq(SysPatient::getStatus, Constants.ENABLE));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysPatients);
    }


}
